﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

public partial class Claims_Claims : System.Web.UI.Page
{
    private List<RejectedItem> rejectedItems;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            rejectedItems = new List<RejectedItem>();
            Session["items"] = rejectedItems;

            RadGridSelectedItems.DataSource = rejectedItems;
            RadGridSelectedItems.DataBind();
           
        }
        else
        {
            rejectedItems = Session["items"] as List<RejectedItem>;
        }
        
    }

    protected void LinkButtonAdd_Click(object sender, EventArgs e)
    {
        var rejectedItem = CreateRejectedItem();
        rejectedItems.Add(rejectedItem);
        Session["items"] = rejectedItems;     

        RadGridSelectedItems.DataSource = rejectedItems;
        RadGridSelectedItems.DataBind();
        
    }

    private Claim CreateClaim()
    {
        int custId = int.Parse(RadComboBoxCustomers.SelectedValue);
        int suppId = int.Parse(RadComboBoxSuppliers.SelectedValue);
        string delyNote = RadTextBoxDailyNote.Text;
        DateTime? arrDate = RadDatePickerArrivalDate.SelectedDate as DateTime?;
        int reqAction = int.Parse(RadComboBoxReqAction.SelectedValue);
        string otherActon = RadTextBoxOtherAction.Text;

        DateTime? claimNoteDate = RadDatePickerClaimNote.SelectedDate as DateTime?;
        string claimNum = RadTextBoxClaimNum.Text;
        string supplier = RadTextBoxSupplier.Text;
        int referenceId = int.Parse(RadComboBoxReference.SelectedValue);
        string orderNum = RadTextBoxOrderNum.Text;

        Claim claim = new Claim()
        {
            CustomerId = custId,
            SupplierId = suppId,
            DelyNote = delyNote,
            ArrivalDate = arrDate,
            RequestedActionId = reqAction,
            OtherAction = otherActon,
            ClaimNoteDate = claimNoteDate,
            ClaimNum = claimNum,
            Supplier = supplier,
            ReferenceId = referenceId,
            OrderNum = orderNum
        };

        return claim;
    }

    private RejectedItem CreateRejectedItem()
    {
        string code = RadTextBoxItemCode.Text;
        string name = RadTextBoxItemName.Text;
        string description = RadTextBoxDescription.Text;
        int? aprvdQn = RadNumericApprovedQuantity.DbValue as int?;
        int? rcvdQn = RadNumericReceivedQuantity.DbValue as int?;
        int? rjctdQn = RadNumericRejectedQuantity.DbValue as int?;
        decimal? price = RadNumericPrice.DbValue as decimal?;
        decimal? extraWork = RadNumericExtraWork.DbValue as decimal?;
        int status = int.Parse(RadComboBoxStatus.SelectedValue);
        decimal? qIC = RadNumericQualityInspCost.DbValue as decimal?;

        RejectedItem item = new RejectedItem()
        {
            ItemCode = code,
            ItemName = name,
            ItemDescription = description,
            ReceivedQuantity = rcvdQn,
            ApprovedQuantity = aprvdQn,
            RejectedQuantity = rjctdQn,
            Price = price,
            ExtraWork = extraWork,
            QualityInspCost = qIC,
            Status = status
        };

        return item;
    }


    protected void RadTextBoxItemCode_TextChanged(object sender, EventArgs e)
    {
        string code = RadTextBoxItemCode.Text;

        if (ItemsDAL.ElementExists(code))
        {
            RadTextBoxItemName.Text = ItemsDAL.GetNameByCode(code);
            RadNumericPrice.DbValue = ItemsDAL.GetPriceByCode(code);
        }
        else
        {
            RadTextBoxItemName.Text = "Няма елемент с такъв код";
            ClearRejectedFields();
        }
    }

    protected void RadGridSelectedItems_DeleteCommand(object source, GridCommandEventArgs e)
    {
        e.Canceled = true;

        int id = e.Item.RowIndex;
        int idToBeDeleted = id / 2 - 1;

        rejectedItems.RemoveAt(idToBeDeleted);
        RadGridSelectedItems.DataSource = rejectedItems;
        Session["items"] = rejectedItems;

        RadGridSelectedItems.DataBind();
    }


    protected void RadGridSelectedItems_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
    {
        string name = e.Column.UniqueName;
        switch (name)
        {

            case "Id":
                e.Column.Visible = false;
                break;
            case "ClaimId":
                e.Column.Visible = false;
                break;
      
        }

        

    }
    protected void LinkButtonAddToDb_Click(object sender, EventArgs e)
    {
        AddToDb();
    }

    protected void LinkButtonPrintEN_Click(object sender, EventArgs e)
    {
        AddToDb();
        int id = ItemsDAL.GetLastClaimId();

        Response.Redirect("~/Claims/PrintEN.aspx?id=" + id.ToString());
    }

    private void AddToDb()
    {
        Claim claim = this.CreateClaim();
        ItemsDAL.InsertClaim(claim);

        int id = ItemsDAL.GetLastClaimId();

        rejectedItems = Session["items"] as List<RejectedItem>;

        for (int i = 0; i != rejectedItems.Count; i++)
        {
            rejectedItems[i].ClaimId = id;
            ItemsDAL.InsertRejectedItem(rejectedItems[i]);
        }

        LinkButtonPrintEN.Text = "INSERTED";
    }

    private void ClearRejectedFields()
    {
        RadTextBoxDescription.Text = string.Empty;
        RadNumericApprovedQuantity.Text = "";
        RadNumericPrice.Text = "";
        RadNumericExtraWork.Text = "";
        RadNumericPrice.Text = "";
        RadNumericRejectedQuantity.Text = "";
        RadNumericReceivedQuantity.Text = "";
        RadNumericQualityInspCost.Text = "";
    }
    
}


/*
GridDropDownColumn column = new GridDropDownColumn();
        column.HeaderText = "Status";
        column.DataSourceID = "Status";
        column.ListValueField = "id";
        column.ListTextField = "status1";
        column.ListDataMember = "id";
        column.UniqueName = "myStatus";
*/